#include "mainwidget.h"
#include "ui_mainwidget.h"

#include <JXQCore/JConcurrent>
#include <JXQCore/JVariant>
#include <JXQCore/JThread>
#include <JXQCore/JDebug>

#include <QElapsedTimer>
#include <QFileDialog>
#include <QMessageBox>

static QElapsedTimer qet;

MainWidget::MainWidget(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::MainWidget),
	pool(100, -1, 10000)
{
	jDebug() << "CPU核心数量:" << JXQ::JThread::getCPUCoreCount();
	JXQ::JConcurrent::setThreadPool(&pool);

	ui->setupUi(this);

	jDebug() << "JThreadPool::maxThreadCount =" << pool.maxThreadCount();
	jDebug() << "JThreadPool::maxQueueLength =" << pool.maxQueueLength();
	jDebug() << "JThreadPool::threadCount =" << pool.threadCount();

	jtimer[0].timeout.subscribe([](){
		jDebug() << "JTimer test 0";
	});
	jtimer[1].timeout.subscribe([](){
		jDebug() << "JTimer test 1";
	});
	jtimer[2].timeout.subscribe([this](){
		ui->label->setText(QString("%1").arg(ui->label->text().toInt()+1));
	});
	jtimer[0].start(700);
	jtimer[1].start(800);
	jtimer[2].start(500);
}

MainWidget::~MainWidget()
{
	jDebug() << "-------------------------------------------------------------";
	delete ui;
}

void MainWidget::on_pb_concurrent_clicked()
{
	auto jc1 = new JXQ::JConcurrent();
	(*jc1)([jc1](){
		delete jc1;
		jDebug() << "world ---";
	},
	[](){
		for(int i=0; i<10; i++)
		{
			jDebug() << "hello - " << i;
			JXQ::JThread::msleep(200);
		}
	});

	auto jc2 = new JXQ::JConcurrent();
	(*jc2)([jc2](){
		delete jc2;
		jDebug() << "concurrent";
	},
	&MainWidget::concurrent, this);

	auto jc3 = new JXQ::JConcurrent();
	(*jc3)([jc3](){
		delete jc3;
		jDebug() << "c o n c u r r e n t 2";
	},
	&MainWidget::concurrent2, this, 0xFF);
}

void MainWidget::on_pb_concurrent_2_clicked()
{
	std::atomic_int i(0);
	auto future = JXQ::JConcurrent::run([&i]()
	{
		for(; i<10; i++)
		{
			jDebug() << "hello - " << i;
			JXQ::JThread::msleep(200);
		}
	});

	QMessageBox box;
	box.show();

	while( future.wait_for(std::chrono::milliseconds(20))
		   == std::future_status::timeout )
	{
		box.setText(QString("%1").arg(i));
		QApplication::processEvents();
	}
}

void MainWidget::concurrent()
{
	for(int i=0; i<14; i++)
	{
		jDebug() << "---------------------- " << i;
		JXQ::JThread::msleep(175);
	}
}

void MainWidget::concurrent2(int v)
{
	for(int i=0; i<20; i++)
	{
		jDebug() << "y s l l ? " << v+i;
		JXQ::JThread::msleep(100);
	}
}

static void func1()
{
	for(int i=0; i<2; i++)
	{
		jDebug() << " --- JThreadPool::thread:" << i;
		JXQ::JThread::msleep(600);
	}
}

void MainWidget::on_pb_threadPool_clicked()
{
	for(int i=0; i<20; i++)
	{
		pool.start(func1);
	}
}

void MainWidget::on_pb_threadPoolInfo_clicked()
{
	jDebug() << "JThreadPool::maxThreadCount =" << pool.maxThreadCount();
	jDebug() << "JThreadPool::maxQueueLength =" << pool.maxQueueLength();
	jDebug() << "JThreadPool::threadCount =" << pool.threadCount();
	jDebug() << "JThreadPool::activeThreadCount =" << pool.activeThreadCount();
}

void MainWidget::on_pb_threadPoolSet_clicked()
{
	pool.setMaxThreadCount(ui->lineEdit_threadNum->text().toInt());
}

void MainWidget::on_pb_threadPoolWait_clicked()
{
	pool.wait();
}
